package com.nep.controller;

import com.nep.entity.Aqi;
import com.nep.entity.AqiFeedback;
import com.nep.entity.ProvinceCity;
import com.nep.entity.Supervisor;
import com.nep.io.FileIO;
import com.nep.service.AqiFeedbackService;
import com.nep.service.impl.AqiFeedbackServiceImpl;
import com.nep.util.CommonUtil;
import com.nep.util.JavafxUtil;
import com.nep.NepsMain;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

public class NepsSelectAqiViewController implements Initializable {
    @FXML
    private TableView<Aqi> txt_tableView;
    @FXML
    private ComboBox<String> txt_province;
    @FXML
    private ComboBox<String> txt_city;
    @FXML
    private TextField txt_address;
    @FXML
    private ComboBox<String> txt_level;
    @FXML
    private TextArea txt_information;
    @FXML
    private Label label_realName;
    
    //主舞台
    public static Stage primaryStage;
    //当前登录成功的公众监督员用户身份
    public static Supervisor supervisor;
    //多态
    private AqiFeedbackService aqiFeedbackService = new AqiFeedbackServiceImpl();

    public TableView<Aqi> getTxt_tableView() {
        return txt_tableView;
    }
    public void setTxt_tableView(TableView<Aqi> txt_tableView) {
        this.txt_tableView = txt_tableView;
    }
    public ComboBox<String> getTxt_province() {
        return txt_province;
    }
    public void setTxt_province(ComboBox<String> txt_province) {
        this.txt_province = txt_province;
    }
    public ComboBox<String> getTxt_city() {
        return txt_city;
    }
    public void setTxt_city(ComboBox<String> txt_city) {
        this.txt_city = txt_city;
    }
    public TextField getTxt_address() {
        return txt_address;
    }
    public void setTxt_address(TextField txt_address) {
        this.txt_address = txt_address;
    }
    public ComboBox<String> getTxt_level() {
        return txt_level;
    }
    public void setTxt_level(ComboBox<String> txt_level) {
        this.txt_level = txt_level;
    }
    public TextArea getTxt_information() {
        return txt_information;
    }
    public void setTxt_information(TextArea txt_information) {
        this.txt_information = txt_information;
    }
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        if (supervisor != null) {
            label_realName.setText(supervisor.getRealName());
        }
        
        //初始化表格
        TableColumn<Aqi, String> levelColumn = new TableColumn<>("级别");
        levelColumn.setMinWidth(80);
        levelColumn.setCellValueFactory(new PropertyValueFactory<>("level"));

        TableColumn<Aqi, String> explainColumn = new TableColumn<>("说明");
        explainColumn.setMinWidth(80);
        explainColumn.setCellValueFactory(new PropertyValueFactory<>("explain"));

        TableColumn<Aqi, String> impactColumn = new TableColumn<>("对健康影响");
        impactColumn.setMinWidth(425);
        impactColumn.setCellValueFactory(new PropertyValueFactory<>("impact"));

        txt_tableView.getColumns().addAll(levelColumn, explainColumn, impactColumn);
        
        try {
            ObservableList<Aqi> data = FXCollections.observableArrayList();
            List<Aqi> alist = (List<Aqi>) FileIO.readObject("aqi.txt");
            if (alist != null) {
                data.addAll(alist);
                txt_tableView.setItems(data);
                
                //初始化AQI等级下拉列表
                txt_level.getItems().clear();
                for (Aqi aqi : alist) {
                    txt_level.getItems().add(aqi.getLevel());
                }
                txt_level.setValue("预估AQI等级");
            }
            
            //初始化省市区域
            List<ProvinceCity> plist = (List<ProvinceCity>) FileIO.readObject("province_city.txt");
            if (plist != null) {
                txt_province.getItems().clear();
                for (ProvinceCity province : plist) {
                    txt_province.getItems().add(province.getProvinceName());
                }
                txt_province.setValue("请选择省区域");
                txt_city.setValue("请选择市区域");
                
                txt_province.valueProperty().addListener((observable, oldValue, newValue) -> {
                    txt_city.getItems().clear();
                    List<String> clist = new ArrayList<>();
                    for (ProvinceCity province : plist) {
                        if (newValue.equals(province.getProvinceName())) {
                            clist = province.getCityName();
                            break;
                        }
                    }
                    txt_city.getItems().addAll(clist);
                    txt_city.setValue("请选择市区域");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
            JavafxUtil.showAlert(primaryStage, "错误", "初始化数据失败", e.getMessage(), "warn");
        }

        txt_city.setOnMouseClicked(event -> {
            if (txt_province.getValue() == null || txt_province.getValue().equals("请选择省区域")) {
                JavafxUtil.showAlert(primaryStage, "提示", "请先选择省区域", "请先选择省区域", "info");
            }
        });
    }

    @FXML
    public void saveFeedBack() {
        // 校验必填项
        if (txt_address.getText() == null || txt_address.getText().trim().isEmpty() ||
            txt_province.getValue() == null || txt_province.getValue().equals("请选择省区域") ||
            txt_city.getValue() == null || txt_city.getValue().equals("请选择市区域") ||
            txt_level.getValue() == null || txt_level.getValue().equals("预估AQI等级") ||
            txt_information.getText() == null || txt_information.getText().trim().isEmpty()) {
            JavafxUtil.showAlert(primaryStage, "警告", "请填写完整信息", "所有字段均为必填项，请补全后再提交", "warn");
            return;
        }

        try {
            AqiFeedback afb = new AqiFeedback();
            afb.setAddress(txt_address.getText());
            afb.setAfName(supervisor.getRealName());
            afb.setProviceName(txt_province.getValue());
            afb.setCityName(txt_city.getValue());
            afb.setEstimateGrade(txt_level.getValue());
            afb.setInfomation(txt_information.getText());
            afb.setDate(CommonUtil.currentDate());
            afb.setState("未指派");
            
            aqiFeedbackService.saveFeedBack(afb);
            JavafxUtil.showAlert(primaryStage, "反馈信息成功", "您的预估AQI信息提交成功", "感谢您的反馈!", "info");
            
            NepsFeedbackViewController.primaryStage = primaryStage;
            JavafxUtil.showStage(NepsMain.class, "/com/nep/view/NepsFeedbackView.fxml", primaryStage, "东软环保公众监督平台-公众监督员端-AQI反馈数据列表");
        } catch (Exception e) {
            e.printStackTrace();
            JavafxUtil.showAlert(primaryStage, "错误", "保存反馈失败", e.getMessage(), "warn");
        }
    }

    @FXML
    public void feedBackList() {
        NepsFeedbackViewController.primaryStage = primaryStage;
        JavafxUtil.showStage(NepsMain.class, "/com/nep/view/NepsFeedbackView.fxml", primaryStage, "东软环保公众监督平台-公众监督员端-AQI反馈数据列表");
    }

    @FXML
    public void back() {
        // 先关闭当前窗口
        Stage stage = (Stage) txt_tableView.getScene().getWindow();
        stage.close();
        // 再打开登录界面
        JavafxUtil.showStage(NepsLoginViewController.class, "/com/nep/view/NepsLoginView.fxml", NepsLoginViewController.primaryStage, "东软环保公众监督平台-公众监督员端");
    }
}
